package com.cooqe.collection_ping.pingfinder;

import com.alibaba.fastjson.JSONObject;
import com.cooqe.basefinder.AbstractBaseFinder;
import com.cooqe.collection_ping.model.Target;
import com.cooqe.collection_ping.utils.PingUtil;
import com.cooqe.entity.ItMetric;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2023/11/16-2023
 * @Description: ping 进行采集
 */
@Component(value = "pingFinderJob")
@Slf4j
@SuppressWarnings("all")
public class PingFinderJob extends AbstractBaseFinder {


    @Override
    //@XxlJob("pingFinderJob")
    public void schedulingExecute(String params) {
        super.schedulingExecute(params);
    }

    @Override
    public void beforeCollection(JSONObject discoverParam) {
        //获取发现参数
        String ip = discoverParam.getString("ip");
        Integer timeout = discoverParam.getInteger("timeout");
        Integer retry = discoverParam.getInteger("retry");

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("ip", ip);
        paramMap.put("timeout", timeout);
        paramMap.put("retry", retry);
        threadLocal.set(paramMap);
    }

    @Override
    public void doCollection(List<ItMetric> metricList) {
        Map<String, Object> discoverParam = threadLocal.get();
        String ip = (String) discoverParam.get("ip");
        Integer timeout = (Integer) discoverParam.get("timeout");
        Integer retry = (Integer) discoverParam.get("retry");
        Target target = new Target(ip, timeout, retry);
        List<Target> targets=new ArrayList<>();
        targets.add(target);
        PingUtil.executePing(targets);
    }

}